/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-points
*
* @File name : ExpirePointsDEventHandle.java
*
* @Author : Administrator
*
* @Date : 2017年8月21日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年8月21日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.points.eventhandle;

import java.util.Date;

import org.marketing.common.dto.EventDataDTO;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.ExpirePointsDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.util.MapConverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ibm.icu.text.SimpleDateFormat;
import com.yonyou.gmmc.service.points.exception.PointsException;
import com.yonyou.gmmc.service.points.service.PointsPayService;
import com.yonyou.gmmc.service.points.util.SpringUtil;

/**
 * @author Administrator
 * @date 2017年8月21日
 */

public class ExpirePointsDEventHandle extends AbstractEventHandle {

    private static Logger       logger  = LoggerFactory.getLogger(ExpirePointsDEventHandle.class);

    // 过期积分
    private static EventBizType BizType = EventConstant.EventBizType.POINTS_EXPIRE;
    // 用户过期积分处理
    private static EventBizStep BizStep = EventConstant.EventBizStep.POINTS_EXPIRE_DEDUCTED;

    @Override
    protected String getHandleInfo() {
        return "用户积分过期清零事件处理";
    }
    
    private String keyCode = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()); 

    /**
     * 主要进行数据转换
     */
    @Override
    protected ExpirePointsDTO preTransform(BizEvent event) {
        ExpirePointsDTO o = new ExpirePointsDTO();
        try {
            o = (ExpirePointsDTO) MapConverUtil.map2POStrict(event.getEventData(), o);
            logger.info(o.toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            this.backInfo = "eventData中的业务数据不能识别：" + e.getMessage();
            throw new PointsException(PointsException.EVENT_CONVERT_FAILD, "eventData中的业务数据不能识别");
        }
        return o;
    }

    /**
     * 积分处理的逻辑
     */
    @Override
    protected boolean pay(EventDataDTO eventData) throws PointsException {
        // 获取到业务数据
        ExpirePointsDTO thisEventData = (ExpirePointsDTO) eventData;

        if (thisEventData.getPointsNum() != null) {

            PointsPayService pointsPayService = (PointsPayService) SpringUtil.getBean("pointsPayService");
            ;
            Long points = thisEventData.getPointsNum();

            // 1.处理积分的事情
            try {
                // 执行积分变动
                pointsPayService.payPointsToUser(-points, thisEventData.getUserId(), thisEventData.getPhone(),
                                                 this.getHandleInfo(), this.keyCode,
                                                 BizStep.getCode(), thisEventData.getPhone());
            } catch (Exception e) {
                logger.warn("执行积分扣减操作失败，将向mq发送积分扣减失败事件", e);
                try {
                    this.backInfo = e.getMessage();
                } catch (Exception e1) {
                    this.backInfo = "积分变动事件调用MQ失败，需要与MQ状态一致，抛出异常，积分扣减不正常，但事务将回滚";
                    logger.error("积分变动事件发送失败", e);
                    throw new PointsException(9001, "积分变动事件发送失败");
                }

            }
        } else {
            this.backInfo = "未找到对应的扣减积分：";
            throw new PointsException(PointsException.RULE_NOTFOUND, "未找到对应的积分");
        }
        return false;
    }

}
